package io.github.enbool.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.github.enbool.bean.ResultBean;
import io.github.enbool.bean.query.ProductQuery;
import io.github.enbool.domain.Product;
import io.github.enbool.service.ProductService;
import io.github.enbool.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/*
 * product api
 * @author enbool
 *
 */
@Api(description = "Product管理接口")
@RestController
@RequestMapping(value = "/api/products")
public class ProductController { 		

	@Autowired
	private ProductService productService;
	
	
	@ApiOperation(value = "查询Product")
	@PostMapping(value = "/query")
	public ResultBean<Page<Product>> query(@RequestBody(required = false) ProductQuery query, Pageable pageable) {
		
		if(query == null) {
			query = new ProductQuery();
		}
		query.setCreateUser(UserUtil.getUser().getId());
		return new ResultBean<>(productService.query(query, pageable));
	}
	
	@ApiOperation(value = "根据id查找Product")
	@GetMapping(value = "/{id:\\d+}")
	public ResultBean<Product> findOne(@PathVariable Long id){
		return new ResultBean<>(productService.findOne(id));
	}
	
	@ApiOperation(value = "新增Product")
	@PostMapping
	public ResultBean<Product> create(@RequestBody Product product){
		return new ResultBean<>(productService.create(product));
	}
	
	@ApiOperation(value = "更新Product")
	@PutMapping
	public ResultBean<Product> update(@RequestBody Product product){
		return new ResultBean<>(productService.update(product));
	}
	
	@ApiOperation(value = "根据id删除Product")
	@DeleteMapping(value = "/{id:\\d+}")
	public ResultBean<Long> delete(@PathVariable Long id){
		return new ResultBean<>(productService.delete(id));
	}
}